package cn.com.b2office.struts2.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.com.b2office.domain.Member;
import cn.com.b2office.domain.User;
import cn.com.b2office.service.MemberService;
import cn.com.b2office.service.UserService;
import cn.com.b2office.util.MD5;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author  zhenggang
 * @version Jul 17, 2008 4:43:44 PM
 * comment
 */
public class ModifyPassWordAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 8175523857555697576L;

	private Map session;
	private MemberService memberService;

	private String regPwd;
	private String newRegPwd;
	private String confirmRegPwd;
	
	private String numPwd;
	private String newNumPwd;
	private String confirmNumPwd;
	private Member member = new Member();
	private UserService userService;
	
	private boolean ifExistMemberno = false;

	//private String memberId="W0806192";  /////
	/** 默认方法修改网上注册密码	*/
	public String execute() throws Exception {
		User user = (User) session.get("user");
		if (user == null) {
			return "login";
		}
		member = memberService.getMember(user.getMemberId());
		member.setRegpwd(MD5.hash(newRegPwd));
		memberService.updateMember(member);
		return SUCCESS;
	}

	/** 修改会员号密码 */
	public String modifyMembernoPwd() {
		User user = (User) session.get("user");
		if (user == null) {
			return "login";
		}
		member = memberService.getMember(user.getMemberId());
		member.setNumpwd(MD5.hash(newNumPwd));
		memberService.updateMember(member);
		return SUCCESS;
	}

	public void validateExecute() {
		User user = (User) session.get("user");
		user = userService.loginByRegName(user.getRegName(), regPwd);
		if(regPwd==null ||regPwd.replace(" ", "").equals("")){
			addFieldError("regPwd", "请输入原密码");
		}else if (user == null) {
			addFieldError("regPwd", "原密码不正确");
		}	
		if(newRegPwd == null || newRegPwd.replace(" ", "").equals("")){
			addFieldError("newRegPwd","请输入新密码新密码");
		}else if(newRegPwd.length()<6 || newRegPwd.length()>18){
			addFieldError("newRegPwd","新密码不符合要求");
		}else if(!confirmRegPwd.equals(newRegPwd)){
			addFieldError("confirmRegPwd", "确认密码不正确");
		}
	}
	
	public void validateModifyMembernoPwd(){
		User user = (User) session.get("user");
		user = userService.loginByMemberno(user.getMemberno(), numPwd);
		if(numPwd ==null || numPwd.replace(" ", "").equals("")){
			addFieldError("numPwd", "请输入原密码");
		}else if (user == null) {
			addFieldError("numPwd", "原密码不正确");
		}
		
		if(newRegPwd == null || newRegPwd.replace(" ", "").equals("")){
			addFieldError("newNumPwd","请输入新密码新密码");
		}else if(newNumPwd.length()<6 || newNumPwd.length()>18){
			addFieldError("newNumPwd", "新密码不符合要求");
		}else if(!confirmNumPwd.equals(newNumPwd)){
			addFieldError("confirmNumPwd", "确认密码不正确");
		}
	}
	
	public String input() {
		User user = (User) session.get("user");
		if (user == null){
			return "login";
		}else{
			member = memberService.getMember(user.getMemberId());
			if(member.getMemberno() != null){
				ifExistMemberno = true;  //会员号存在
			}
			//System.out.println("member.getMemberno() = "+member.getMemberno());
		}
		return INPUT;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setRegPwd(String regPwd) {
		this.regPwd = regPwd;
	}

	public void setNumPwd(String numPwd) {
		this.numPwd = numPwd;
	}

	public void setConfirmRegPwd(String confirmRegPwd) {
		this.confirmRegPwd = confirmRegPwd;
	}

	public void setConfirmNumPwd(String confirmNumPwd) {
		this.confirmNumPwd = confirmNumPwd;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setNewRegPwd(String newRegPwd) {
		this.newRegPwd = newRegPwd;
	}

	public void setNewNumPwd(String newNumPwd) {
		this.newNumPwd = newNumPwd;
	}

	public boolean isIfExistMemberno() {
		return ifExistMemberno;
	}
}
